$(function() {
    $('#btn').click(function() {
        let username = $('#username').val();
        let password = $('#password').val();
        let Messagestate = "0";
        console.log(username)
        if ($('#yzm').is(':checked')) {
            Messagestate = "1";
            console.log(username)
            if (!checkTelephone(username)) {
                alert("手机号格式错误！！！")
                return;
            }
        }
        onLoginSubmit(username,password,Messagestate);
    });
    $('#reqMSM').click(function() {
        let username = $('#username').val();
        onLoginSubmit(username,"","1");
    });
});

function onLoginSubmit(username,password,Messagestate) {
    var response = grecaptcha.getResponse();
    response = 1;
    if (response.length === 0) {
        // 如果验证码未通过验证
        alert("请完成验证码验证！");
        $('#g-recaptcha').css('display', 'block');
    } else {
        // 如果验证码通过验证
        // 在这里添加发送登录请求的逻辑
        console.log(username+'请求登录');
        if (password == null || password == "" || password.length == 0){
            if (Messagestate==1) {
                reqMSM(username)
            }
            return;
        }
        login(username, password, Messagestate);

    }
}

function login(username,password,messagestate){
    let url = HTTPURL+"/Login";
    console.log(url)
    $.ajax({
        url: url,
        method: 'Post',
        contentType: 'application/json',
        data:  JSON.stringify({
            loginName: username,
            password: password,
            messagestate: messagestate
        }),
        success: function(response) { // 请求成功时的回调函数
           if(response.code!==null && response.code==200){
               setTimeout(function() {
                   // 保存用户名
                   localStorage.setItem("username", username);
                   // 保存 token
                   if (response.data && response.data.token) {
                       localStorage.setItem("token", response.data.token);
                       console.log("Token 已保存:", response.data.token);
                   }
                   // 保存用户信息
                   if (response.data && response.data.user) {
                       if (messagestate == 1){
                           localStorage.setItem(username, response.data.user.password+"￣￣"+"验证码默认用户");
                       }else {
                           localStorage.setItem(username, response.data.user.password);
                       }
                   }
                   target("../../../chat/index.html"); //GPT聊天
               }, 0);
               $('.eye-ball').css({
                   'background-color': '#0cff00'
               });
           }else{
               if (response.code){
                   alert(response.msg)
               }else{
                   alert("未知异常")
               }
               $('.eye-ball').css({
                   'background-color': '#FF0000'
               });
           }
        },
        error: function(xhr, status, error) { // 请求失败时的回调函数
            $('.eye-ball').css({
                'background-color': '#FF0000'
            });
            console.log("登录请求失败了：")
            console.log(error)
        }
    });
}
function reqMSM(username,password){
    let url = HTTPURL+"/reqMSM";
    $.ajax({
        url: url, // 请求的URL
        method: 'Get', // 请求方法（GET、POST等）
        dataType: 'json', // 期望的响应数据类型（json、html等）
        data: { // 请求参数
            username:username
        },
        success: function(response) { // 请求成功时的回调函数
            $('.eye-ball').css({
                'background-color': '#0cff00'
            });
            console.log(response)
            // 判断返回是否为空或空字符串
            if (response.code == 500) {
                alert(response.msg);
            }

        },
        error: function(xhr, status, error) { // 请求失败时的回调函数
            console.log("发送请求失败了：")
            console.log(error)
        }
    });
}


// 手机号验证
function checkTelephone(telephone) {
    var reg=/^[1][3,4,5,6,7,8][0-9]{9}$/;
    if (!reg.test(telephone)) {
        return false;
    } else {
        return true;
    }
}
function target(url){
    if(url.length > 1){
        window.location.href = url;
    }else{
        window.location.href = 'aaaa/login/code/115/115.html';
    }

}
